﻿Public Class CommandMode

    Private Sub CommandMode_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Main.Show() 'Uppon Closing, restore main form

    End Sub

    Private Sub cmdExec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExec.Click
        Dim cmdQuery As String = cmdStatementBox.Text 'Variable gets Statement's box, contents
        Dim forbidenWords() As String = {"insert", "INSERT", "update", "UPDATE", "drop", "DROP", "delete", "DELETE", "constraint", "CONSTRAINT", "create", "CREATE"} 'Forbidden Statements

        For parser As Integer = 0 To forbidenWords.Length - 1 'For-Loop to search all the "forbidden" words in the array
            If cmdStatementBox.Text.Contains(forbidenWords(parser)) Then 'if one of theese is contained inside the cmdStatementBox....
                MsgBox("Non-Select Queries Are Not Allowed", MsgBoxStyle.Exclamation) 'Notify User...
                cmdStatementBox.Clear() 'Clear the QueryBox,not to provoke an exception
                Return 'and get out of the Function immidiatelly
            End If
        Next parser

        Main.executeQuery(cmdQuery) 'Calls Main Forms execute query sub which will fill the datagrid if statement is valid

    End Sub

    Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
        cmdStatementBox.Clear() 'Empty the statement box
    End Sub

    Private Sub cmdStatementBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStatementBox.TextChanged

        'Syntax Highlighting needs improvement, use it at your own risk
        'For safety reasons, it's deactivated bu default

        If syntaxCheck.CheckState = CheckState.Checked Then 'if Syntax Highlight Checkbox is active/checked

            Try 'Try, to be sure....

                Dim reserveds As ArrayList = New ArrayList() 'Reserved Words
                reserveds.Add("select")
                reserveds.Add("SELECT")
                reserveds.Add("from")
                reserveds.Add("FROM")
                reserveds.Add("where")
                reserveds.Add("WHERE")
                reserveds.Add("order")
                reserveds.Add("ORDER")
                reserveds.Add("by")
                reserveds.Add("BY")
                reserveds.Add("as")
                reserveds.Add("AS")
                reserveds.Add("join")
                reserveds.Add("JOIN")
                reserveds.Add("exists")
                reserveds.Add("EXISTS")
                reserveds.Add("not")
                reserveds.Add("NOT")
                reserveds.Add("or")
                reserveds.Add("OR")
                reserveds.Add("and")
                reserveds.Add("AND")
                reserveds.Add("distinct")
                reserveds.Add("DISTINCT")
                reserveds.Add("on")
                reserveds.Add("ON")
                reserveds.Add("asc")
                reserveds.Add("ASC")
                reserveds.Add("desc")
                reserveds.Add("DESC")


                Dim index As Integer 'Index at richtextbox
                Dim cursorPos As Integer 'cursor position
                Dim beforeColor As Color = cmdStatementBox.SelectionColor 'Previus Color for restoring

                cursorPos = cmdStatementBox.SelectionStart 'get the selected text start position

                For Each reservedWord As String In reserveds 'For each reserved word

                    index = cmdStatementBox.Text.IndexOf(reservedWord) 'index of word inside the textbox

                    If index <> -1 Then 'if index is not -1 (not an index)
                        cmdStatementBox.Select(index, reservedWord.Length) 'select the word...
                        cmdStatementBox.SelectionColor = Color.Blue 'change the color of it
                        cmdStatementBox.DeselectAll() 'deselect the word
                        cmdStatementBox.SelectionStart = cursorPos + 1 'start again one position far from the current cursor pos
                    End If
                Next

                cmdStatementBox.SelectionColor = beforeColor 'restore normal color

            Catch ex As Exception 'You know, just in case
                Exit Sub 'leave without notice!
            End Try

        Else
            cmdStatementBox.SelectionColor = Color.Black 'keep color black
        End If


    End Sub

    Private Sub CommandMode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        syntaxCheck.CheckState = CheckState.Unchecked 'By-default syntax-highlighting is disabled
    End Sub

    Private Sub restoreMain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles restoreMain.Click
        Me.Close() 'Close me 
        Main.Show() 'and restore main form
    End Sub
End Class